package com.zhhs.galaxyott.adapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.movieDetailActivity.MovieDetailActivity;
import com.zhhs.galaxyott.beans.HotMoviesBean;
import com.zhhs.galaxyott.utils.Logs;

public class HotMovieGridAdapter extends BaseAdapter implements OnClickListener {

    private Context mContext;
    private List<HotMoviesBean> appList;
    private LayoutInflater inflater;
    private ICallback mCallback;

    public HotMovieGridAdapter(Activity context, List<HotMoviesBean> appList, ICallback mCallback) {
        this.inflater = LayoutInflater.from(context);
        this.appList = appList;
        this.mContext = context;

        this.mCallback = mCallback;
    }

    @Override
    public int getCount() {
        return appList.size();
    }

    @Override
    public Object getItem(int position) {
        return appList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View contentView, ViewGroup parent) {
        ViewHolder holder = null;
        if (contentView == null) {
            holder = new ViewHolder();
            contentView = inflater.inflate(R.layout.grid_item, null);
            holder.tv_title = (TextView) contentView.findViewById(R.id.tv_title);
            holder.tiv_icon = (ImageView) contentView.findViewById(R.id.tiv_icon);
            contentView.setTag(holder);
        } else {
            holder = (ViewHolder) contentView.getTag();
        }

        HotMoviesBean bean = appList.get(position);
        holder.tv_title.setText(bean.getName());
        // holder.tv_title.setSelected(true);
        // holder.tiv_icon.configImageUrl(app.imageUrl);

        holder.tiv_icon.setTag(R.id.tiv_icon, position);
        Glide.with(mContext).load(bean.getImgurl()).placeholder(R.drawable.movie_item_default).crossFade()
                .into(holder.tiv_icon);

        holder.tiv_icon.setOnClickListener(this);

        holder.tiv_icon.setOnFocusChangeListener(mOnFocusChangeListener);
        return contentView;
    }

    OnFocusChangeListener mOnFocusChangeListener = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasfocus) {

            if (hasfocus) {
                mCallback.setText((Integer) v.getTag(R.id.tiv_icon));
                ((TextView) ((LinearLayout) v.getParent()).getChildAt(1)).setSelected(true);

            } else {
                ((TextView) ((LinearLayout) v.getParent()).getChildAt(1)).setSelected(false);
            }

        }
    };

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(mContext, MovieDetailActivity.class);
        Logs.logD("DEBUG_AD", "THIS IS -->POSITION:" + v.getTag());
        intent.putExtra("movieid", appList.get((Integer) v.getTag(R.id.tiv_icon)).getId());
        mContext.startActivity(intent);

    }

    static class ViewHolder {
        TextView tv_title;
        ImageView tiv_icon;
    }
}
